/**
 * Created by Administrator on 2018/7/29/029.
 */
layui.use(['table', 'form', 'layer','element'], function () {

    // 操作对象
    var form = layui.form,
        table = layui.table,
        layer = layui.layer,
        element = layui.element,
        $ = layui.jquery;


    var token = $('#_token').val();
    var tableTimeIns;
    var tableNumIns;
    var pack_index = 0;
    var u_param = GetRequest();     //url参数
    var check_val = u_param.flag;  //冻结状态 0 正常 1 冻结
    $('#user_id').val(u_param.uuid);

    // 初始化
    getMemeberDetail();
    if(check_val == 1){
        $('.frozen').html('解除冻结');
    }

    //时长套餐操作
    table.on('tool(time_pack_data)', function(obj) {

        //续费
        if(obj.event === 'renew') {
            if(obj.data.set_meal_id == 6){
                layer.msg('请用户明日重新领取测试套餐');
            }else{
                var index = layer.load(2, {
                    shade: [0.1,'#fff'] //0.1透明度的白色背景
                    , time: 1000,
                    end:function(e){
                        //自定页
                        layer.open({
                            type: 1,
                            title:['续费','font-size: 16px;color: #000;font-weight: bolder;'],
                            skin: 'layui-layer-rim', //加上边框
                            area: ['422px', '360px'], //宽高
                            content: $('.renew'),
                            success:function(){
                                getOrderNumber(1);
                                $('#p_id').val(obj.data.id);

                            }
                        });
                    }
                });
            }

        }

        //修改
        if(obj.event === 'edit_time_pack') {
            if(obj.data.set_meal_id == 6){
                layer.msg('请用户明日重新领取测试套餐');
            }else{
                var index = layer.load(2, {
                    shade: [0.1,'#fff'] //0.1透明度的白色背景
                    , time: 1000,
                    end:function(e){
                        //自定页
                        layer.open({
                            type: 1,
                            title:['时长套餐修改','font-size: 16px;color: #000;font-weight: bolder;'],
                            skin: 'layui-layer-rim', //加上边框
                            area: ['422px', '530px'], //宽高
                            content: $('.edit_time'),
                            success:function(){
                                getOrderNumber(2);
                                $('#m_id').val(obj.data.id);
                                $('#day_num').val(obj.data.daily_limit_num);
                                $('#time_num').val(obj.data.request_num);
                                $('#req_time').val(obj.data.request_time);
                            }
                        });
                    }
                });
            }
        }


    });

    //包量套餐操作
    table.on('tool(num_pack_data)', function(obj) {

        //详情
        if(obj.event === 'add_num') {
            var index = layer.load(2, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
                , time: 1000,
                end:function(e){
                    //自定页
                    layer.open({
                        type: 1,
                        title:['续费','font-size: 16px;color: #000;font-weight: bolder;'],
                        skin: 'layui-layer-rim', //加上边框
                        area: ['422px', '360px'], //宽高
                        content: $('.add_num'),
                        success:function(){
                            getOrderNumber(3);
                            $('#n_id').val(obj.data.id);
                        }
                    });
                }
            });

        }

        //充值动作
        if(obj.event === 'edit_num_pack') {
            var index = layer.load(2, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
                , time: 1000,
                end:function(e){
                    //自定页
                    layer.open({
                        type: 1,
                        title:['包量套餐修改','font-size: 16px;color: #000;font-weight: bolder;'],
                        skin: 'layui-layer-rim', //加上边框
                        area: ['424px', '460px'], //宽高
                        content: $('.edit_num'),
                        success:function(){
                            getOrderNumber(4);
                            $('#e_id').val(obj.data.id);
                            $('#time_num1').val(obj.data.request_num);
                            $('#req_time1').val(obj.data.request_time);
                        }
                    });
                }
            });
        }


    });

    //刷新
    $('#btn-refresh').on('click', function () {
        if(pack_index == 0){
            tableTimeIns.reload();
        }
        if(pack_index == 1){
            tableNumIns.reload();
        }

    });

    //重置密码
    $('.reset').on('click',function(){
        //自定页
        layer.open({
            type: 1,
            title:['重置密码','font-size: 16px;color: #000;font-weight: bolder;'],
            skin: 'layui-layer-rim', //加上边框
            area: ['380px', '250px'], //宽高
            content: $('.reset_pass'),
            success:function(){

            }
        });
    });

    //充值点击
    $('.recharge').on('click',function () {
        var index = layer.load(2, {
            shade: [0.1,'#fff'] //0.1透明度的白色背景
            , time: 1000,
            end:function(e){
                //自定页
                layer.open({
                    type: 1,
                    title:['充值','font-size: 16px;color: #000;font-weight: bolder;'],
                    skin: 'layui-layer-rim', //加上边框
                    area: ['400px', '360px'], //宽高
                    content: $('.acc_recharge'),
                    success:function(){
                        getOrderNumber(5);
                    }
                });
            }
        });
    });

    //冻结点击
    $('.frozen').on('click',function () {
        layer.open({
            type: 1,
            title:['修改会员状态','font-size: 16px;color: #000;font-weight: bolder;'],
            skin: 'layui-layer-rim', //加上边框
            area: ['240px', '200px'], //宽高
            content: $('.acc_update'),
            success:function(){
                $('[name=frozen_flag]').each(function(i,item){
                    if($(item).val()==check_val){
                        $(item).prop('checked',true);
                        form.render();
                    }
                });

            }
        });
    });


    //添加套餐点击
    $('.set_meal_add').on('click',function () {
        layer.open({
            type: 1,
            title:['套餐添加','font-size: 16px;color: #000;font-weight: bolder;'],
            skin: 'layui-layer-rim', //加上边框
            area: ['424px', '470px'], //宽高
            content: $('.add_time_package'),
            success:function(){
                getOrderNumber(6);
            }
        });
    });


    //充值提交
    form.on('submit(acc_recharge)', function (data) {
        var recharge = data.field;
        if(check_val == 1){
            layer.msg('此用户已冻结,请解冻后操作',{
                time: 2000,
                end:function(e){
                    layer.closeAll('page');
                    getMemeberDetail()
                }
            });
            return;
        }

        accRecharge(recharge)

    });

    //重置密码提交
    form.on('submit(reset_pass)', function (data) {
        var data = data.field;

        var passreg  = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/;

        if(!passreg.test(data.password)) {
            layer.msg('密码必须为8-20位字母与数字组合密码',{anim:6,icon:5},function(){});
            return false;
        }


        if(data.confirm_password != data.password){
            layer.msg('两次密码不一致',{anim:6,icon:5},function(){});
            return false;
        }

        resetPassword(data);

    });

    //状态弹窗 提交
    form.on('submit(acc_update)', function (data) {
        var update = data.field;

        if(check_val == update.frozen_flag){
            layer.closeAll('page');
            return;
        }
        if(update.frozen_flag == 1){
            layer.confirm('确定冻结此账号？冻结以后该账号将无法使用,请谨慎操作', {
                btn: ['仍要冻结','取消'] //按钮
            }, function(){
                forbiddenAccount(update);
            })

        }
        if(update.frozen_flag == 0){
            thawAccount(update);
        }
    });

    //续费提交
    form.on('submit(acc_renew)', function (data) {
        var acc_renew = data.field;
        if(check_val == 1){
            layer.msg('此用户已冻结,请解冻后操作',{
                time: 2000,
                end:function(e){
                    layer.closeAll('page');
                    getTimePackList()
                }
            });
            return;
        }

        accRenew(acc_renew)

    });

    //加量提交
    form.on('submit(add_num)', function (data) {
        var add_num = data.field;
        if(check_val == 1){
            layer.msg('此用户已冻结,请解冻后操作',{
                time: 2000,
                end:function(e){
                    layer.closeAll('page');
                    getNumPackList()
                }
            });
            return;
        }

        accRenew(add_num)

    });

    //修改时长套餐参数提交
    form.on('submit(edit_time)', function (data) {
        var edit_time = data.field;
        if(check_val == 1){
            layer.msg('此用户已冻结,请解冻后操作',{
                time: 2000,
                end:function(e){
                    layer.closeAll('page');
                    getTimePackList()
                }
            });
            return;
        }

        updateSetMeal(edit_time)

    });

    //修改包量套餐参数提交
    form.on('submit(edit_num)', function (data) {
        var edit_num = data.field;
        if(check_val == 1){
            layer.msg('此用户已冻结,请解冻后操作',{
                time: 2000,
                end:function(e){
                    layer.closeAll('page');
                    getNumPackList()
                }
            });
            return;
        }

        updateSetMeal(edit_num)

    });

    //添加用户购买套餐提交
    form.on('submit(add_package)', function (data) {
        var recharge = data.field;
        if(check_val == 1){
            layer.msg('此用户已冻结,请解冻后操作',{
                time: 2000,
                end:function(e){
                    layer.closeAll('page');
                }
            });
            return;
        }

        addPackage(recharge)

    });

    //会员详情tab
    element.on('tab(docDemoTabBrief)', function(data){
        //套餐信息
        if(data.index == 1){
            getTimePackList()
        }
        //钱包流水
        if(data.index == 2){
            getWalletList();
        }

    });

    //套餐信息tab
    element.on('tab(package)', function(data){
        pack_index = data.index;

        //时长套餐信息
        if(data.index == 0){
            getTimePackList()
        }

        //包量套餐信息
        if(data.index == 1){
            getNumPackList()
        }

    });

    //获取url参数
    function GetRequest() {
        var url = location.search; //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            strs = str.split("&");
            for(var i = 0; i < strs.length; i ++) {
                theRequest[strs[i].split("=")[0]]=decodeURI(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }

    //用户详情
    function getMemeberDetail(){
        var u_id = u_param.uuid;
        $.ajax({
            url: '/getAccDetail',          //  删除账号
            type: 'post',
            data: {
                u_id: u_id,
                _token:token,
            },
            timeout: 5000,
            dataType: 'json',
            success: function (data, textStatus, jqXHR) {
                if (data.error_code == 0) {
                    var userinfo = data.data;
                    $("#username").val(userinfo.username);
                    $("#mobile").val(userinfo.mobile);
                    $("#wechat").val(userinfo.wechat_bind_name);
                    $("#balance").val(userinfo.balance);
                    $("#status").val(userinfo.frozen_status);
                    $("#from").val(userinfo.source_type_name);
                    $("#remark").val(userinfo.remark);
                    check_val = userinfo.frozen_flag;
                    if(check_val == 1){
                        $('.frozen').html('解除冻结');
                    } else{
                        $('.frozen').html('冻结用户');
                    }

                }
            },
            error:function(){
                layer.msg('出错了',{
                    time: 2000,
                    end:function(e){
                        location.href = "/acc_detail"
                    }
                });
            }
        });
    }

    //流水号
    function getOrderNumber(action_type){
        $.ajax({
            url: '/getAdminOrder',          //  流水号
            type: 'get',
            data: {},
            timeout: 3000,
            dataType: 'json',
            success: function (data, textStatus, jqXHR) {
                if (data.error_code == 0) {

                    switch(action_type)
                    {
                        case 1:
                            $('#pro_order_renew').val(data.data);   //续费
                            break;
                        case 2:
                            $('#pro_order_edit').val(data.data);    //修改时长套餐参数
                            break;
                        case 3:
                            $('#re_order_add_num').val(data.data);      //加量
                            break;
                        case 4:
                            $('#re_order_edit').val(data.data);         //修改按量套餐参数
                            break;
                        case 5:
                            $('#serial_number').val(data.data);           //充值
                            break;
                        case 6:
                            $('#user_meal_add').val(data.data);         //为用户添加套餐
                            break;
                        default:
                            break;
                    }

                }
            }
        });

    }

    //会员充值
    function accRecharge(obj){

        obj._token = token;
        obj.u_id = u_param.uuid;

        $.ajax({
            url: '/accRecharge',          //  充值
            type: 'post',
            data: obj,
            timeout: 5000,
            dataType: 'json',
            success: function (data, textStatus, jqXHR) {
                if (data.error_code == 0) {
                    layer.msg('充值成功',{
                        time: 2000,
                        end:function(e){
                            layer.closeAll('page');
                            getMemeberDetail()
                        }});

                } else {
                    layer.msg('充值失败，请重新操作',{
                        time: 2000,
                        end:function(e){
                            layer.closeAll('page');
                            getMemeberDetail()
                        }});

                }
            },
            error:function(){
                layer.msg('出错了',{
                    time: 2000,
                    end:function(e){
                        layer.closeAll('page');
                        getMemeberDetail()
                    }});
            }
        });
    }

    //会员状态修改--冻结
    function forbiddenAccount(obj) {
        obj.u_id = u_param.uuid;
        var for_data = obj;
        $.ajax({
            url: '/forbiddenMem',          //  删除账号
            type: 'post',
            data: {
                u_id: for_data.u_id,
                _token:token,
            },
            timeout: 5000,
            dataType: 'json',
            success: function (data, textStatus, jqXHR) {
                if (data.error_code == 0) {
                    layer.msg('冻结成功',{
                        time: 2000,
                        end:function(e){
                            layer.closeAll('page');
                            getMemeberDetail()
                        }});

                } else {
                    layer.msg('冻结失败',{
                        time: 2000,
                        end:function(e){
                            layer.closeAll('page');
                            getMemeberDetail()
                        }});
                }
            },
            error:function(){
                layer.msg('出错了',{
                    time: 2000,
                    end:function(e){
                        layer.closeAll('page');
                        getMemeberDetail()
                    }});
            }
        });
    }

    //会员状态修改--解冻
    function thawAccount(obj){
        obj.u_id = u_param.uuid;
        var for_data = obj;
        $.ajax({
            url: '/thawMember',          //  删除账号
            type: 'post',
            data: {
                u_id: for_data.u_id,
                _token:token,
            },
            timeout: 5000,
            dataType: 'json',
            success: function (data, textStatus, jqXHR) {
                if (data.error_code == 0) {
                    layer.msg('解除冻结成功',{
                        time: 2000,
                        end:function(e){
                            layer.closeAll('page');
                            getMemeberDetail()
                        }});
                } else {
                    layer.msg('解除冻结失败',{
                        time: 2000,
                        end:function(e){
                            layer.closeAll('page');
                            getMemeberDetail()
                        }});
                }
            },
            error:function(){
                layer.msg('出错了',{
                    time: 2000,
                    end:function(e){
                        layer.closeAll('page');
                        getMemeberDetail()
                    }});
            }
        });
    }

    //重置密码
    function resetPassword(data){
        data._token = token;
        data.u_id = u_param.uuid;
        console.log(data);
        $.ajax({
            url: '/resetAccPassword',          //  删除账号
            type: 'post',
            data: data,
            timeout: 5000,
            dataType: 'json',
            success: function (data, textStatus, jqXHR) {
                if (data.error_code == 0) {
                    layer.msg('重置密码成功',{
                        time: 2000,
                        end:function(e){
                            layer.closeAll('page');
                            getMemeberDetail()
                        }});
                } else {
                    layer.msg('重置密码失败',{
                        time: 2000,
                        end:function(e){
                            layer.closeAll('page');
                            getMemeberDetail()
                        }});
                }
            },
            error:function(){
                layer.msg('出错了',{
                    time: 2000,
                    end:function(e){
                        layer.closeAll('page');
                        getMemeberDetail()
                    }});
            }
        });
    }

    //钱包流水
    function getWalletList(){
        table.render({
            elem: '#wallet_data',
            page: true,
            count: 100,
            limit: 10,
            cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
            jump: function(obj){

            },
            cols: [[                  //标题栏
                {field: 'order_no', title: '订单号'}
                , {field: 'money', title: '金额'}
                , {field: 'type_name', title: '消费类型'}
                , {field: 'created_at', title: '订单创建时间'}
                , {field: 'pay_channel', title: '支付方式'}

            ]],
            url: '/getUserWalletList',
            where: {
                u_id : u_param.uuid,
                _token: token
            },    //查询参数
            method: 'post',
            done: function (res, curr, count) {

            }
        });
    }

    //时长套餐列表
    function getTimePackList(){
        tableTimeIns = table.render({
            elem: '#time_pack_data',
            page: true,
            count: 100,
            limit: 10,
            cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
            jump: function(obj){

            },
            cols: [[                  //标题栏
                {field: 'product_name', title: '套餐名称'}
                , {field: 'daily_limit_num', title: '日提取量'}
                , {field: 'daily_limit_use', title: '已提取量'}
                , {field: 'request_num', title: '单次最大提取'}
                , {field: 'request_time', title: '提取频率(秒/次)'}
                , {field: 'status', title: '套餐状态'}
                , {field: 'expired_at', title: '到期时间'}
                , {fixed: 'right', title: '操作', width: 160, align: 'center', toolbar: '#renew'} //这里的toolbar值是模板元素的选择器

            ]],
            url: '/getUserSetMealTimeList',
            where: {
                u_id : u_param.uuid,
                _token: token
            },    //查询参数
            method: 'post',
            done: function (res, curr, count) {

            }
        });
    }

    //包量套餐列表
    function getNumPackList(){
        tableNumIns = table.render({
            elem: '#num_pack_data',
            page: true,
            count: 100,
            limit: 10,
            cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
            layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
            jump: function(obj){

            },
            cols: [[                  //标题栏
                {field: 'product_name', title: '套餐名称'}
                , {field: 'total', title: 'ip总量'}
                , {field: 'is_use', title: '已使用量'}
                , {field: 'surplus', title: '剩余量'}
                , {field: 'request_num', title: '单次最大提取'}
                , {field: 'request_time', title: '提取频率(秒/次)'}
                , {fixed: 'right', title: '操作', width: 160, align: 'center', toolbar: '#edit_num_pack'} //这里的toolbar值是模板元素的选择器

            ]],
            url: '/getUserSetMealNumList',
            where: {
                u_id : u_param.uuid,
                _token: token
            },    //查询参数
            method: 'post',
            done: function (res, curr, count) {

            }
        });
    }

    //会员续费、加量
    function accRenew(obj){

        obj._token = token;
        obj.u_id = u_param.uuid;

        $.ajax({
            url: '/renewSetMeal',          //  续费,加量
            type: 'post',
            data: obj,
            timeout: 5000,
            dataType: 'json',
            success: function (data, textStatus, jqXHR) {
                if (data.error_code == 0) {
                    layer.msg('操作成功',{
                        time: 2000,
                        end:function(e){
                            layer.closeAll('page');
                            if(obj.set_meal_type == 1){
                                getTimePackList()
                            }else{
                                getNumPackList()
                            }

                        }});

                } else {
                    layer.msg(data.error_message,{
                        time: 2000,
                        end:function(e){
                            layer.closeAll('page');
                            if(obj.set_meal_type == 1){
                                getTimePackList()
                            }else{
                                getNumPackList()
                            }
                        }});

                }
            },
            error:function(){
                layer.msg('出错了',{
                    time: 2000,
                    end:function(e){
                        layer.closeAll('page');
                        if(obj.set_meal_type == 1){
                            getTimePackList()
                        }else{
                            getNumPackList()
                        }
                    }});
            }
        });
    }

    //套餐参数修改
    function updateSetMeal(obj){

        obj._token = token;
        obj.u_id = u_param.uuid;

        $.ajax({
            url: '/updateSetMeal',          //  修改
            type: 'post',
            data: obj,
            timeout: 5000,
            dataType: 'json',
            success: function (data, textStatus, jqXHR) {
                if (data.error_code == 0) {
                    layer.msg('操作成功',{
                        time: 2000,
                        end:function(e){
                            layer.closeAll('page');
                            if(obj.set_meal_type == 1){
                                getTimePackList()
                            }else{
                                getNumPackList()
                            }

                        }});

                } else {
                    layer.msg(data.error_message,{
                        time: 2000,
                        end:function(e){
                            layer.closeAll('page');
                            if(obj.set_meal_type == 1){
                                getTimePackList()
                            }else{
                                getNumPackList()
                            }
                        }});

                }
            },
            error:function(){
                layer.msg('出错了',{
                    time: 2000,
                    end:function(e){
                        layer.closeAll('page');
                        if(obj.set_meal_type == 1){
                            getTimePackList()
                        }else{
                            getNumPackList()
                        }
                    }});
            }
        });
    }

    //添加用户套餐
    function addPackage(obj){
        obj._token = token;
        obj.u_id = u_param.uuid;

        $.ajax({
            url: '/addPackage',          //  修改
            type: 'post',
            data: obj,
            timeout: 5000,
            dataType: 'json',
            success: function (data, textStatus, jqXHR) {
                if (data.error_code == 0) {
                    layer.msg('操作成功',{
                        time: 2000,
                        end:function(e){
                            layer.closeAll('page');
                            if(obj.set_meal_type == 1){
                                getTimePackList()
                            }else{
                                getNumPackList()
                            }

                        }});

                } else {
                    layer.msg(data.error_message,{
                        time: 2000,
                        end:function(e){
                            layer.closeAll('page');
                            if(obj.set_meal_type == 1){
                                getTimePackList()
                            }else{
                                getNumPackList()
                            }
                        }});

                }
            },
            error:function(){
                layer.msg('出错了',{
                    time: 2000,
                    end:function(e){
                        layer.closeAll('page');
                        if(obj.set_meal_type == 1){
                            getTimePackList()
                        }else{
                            getNumPackList()
                        }
                    }});
            }
        });
    }

});